Code
Assignment operators
# Assign
= 5
x x
5
Code
# Add and assign
+= 3
x x
8
Code
# Subtract and assign
-= 2
x x
6
Code
# Multiplication and assign
*= 2
x x
12
Operators are the building blocks of any programming language, and understanding how to use them effectively is crucial for writing efficient and readable code in Python.
Operators in Python are special symbols that perform arithmetic or logical computation. The value that the operator operates on is called the operand. Python operators can be classified into several categories:
Assignment operators are used to assign values to variables:
=
Assigns a value to a variable. E.g., x = 5
+=
Adds right operand to the left operand and assign the result to left operand. E.g., x += 5
-=
Subtracts right operand from the left operand and assign the result to left operand. E.g., x -= 5
*=
and others similarly modify and assign.Assignment operators
# Assign
= 5
x x
5
# Add and assign
+= 3
x x
8
# Subtract and assign
-= 2
x x
6
# Multiplication and assign
*= 2
x x
12
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.
+
Addition: Adds two operands. E.g., x + y
-
Subtraction: Subtracts the right operand from the left operand. E.g., x - y
*
Multiplication: Multiplies two operands. E.g., x * y
/
Division: Divides the left operand by the right operand. E.g., x / y
%
Modulus: Returns the remainder when the left operand is divided by the right operand. E.g., x % y
**
Exponent: Left operand raised to the power of right operand. E.g., x ** y
//
Floor Division: Division that results into whole number adjusted to the left in the number line. E.g., x // y
Python Arithmetic operators
# Addition
5 + 3
8
# Subtraction
10 - 3
7
# Multiplication
4 * 3
12
# Division
8 / 2
4.0
# Modulus
9 % 4
1
# Exponent
2 ** 3
8
# Floor Division
17 // 4
4
Comparison operators are used to compare values. It either returns True or False according to the condition.
==
Equal: True if both operands are equal. E.g., x == y
!=
Not equal: True if operands are not equal. E.g., x != y
>
Greater than: True if left operand is greater than the right operand. E.g., x > y
<
Less than: True if left operand is less than the right operand. E.g., x < y
>=
Greater than or equal to: True if left operand is greater than or equal to the right operand. E.g., x >= y
<=
Less than or equal to: True if left operand is less than or equal to the right operand. E.g., x <= y
Python Comparison Operators
# Equal
5 == 3
False
# Not equal
5 != 3
True
# Greater than
5 > 3
True
# Less than
5 < 3
False
# Greater than or equal to
5 >= 5
True
# Less than or equal to
5 <= 3
False
Logical operators are used to combine conditional statements:
and
Returns True if both statements are true. E.g., x < 5 and x < 10
or
Returns True if one of the statements is true. E.g., x < 5 or x < 4
not
Reverse the result, returns False if the result is true. E.g., not(x < 5 and x < 10)
Python Logical operators
# and
5 > 3) and (5 > 4) (
True
# or
5 > 3) or (5 < 4) (
True
# not
not(5 > 3)
False